﻿using KinectWindows.Math;
using KinectWindows.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KinectWindows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {

        public Stack<NuiWindow> NuiWindowStack = new Stack<NuiWindow>();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Grid_Loaded_1(object sender, RoutedEventArgs e)
        {
           // Setup all controls and dimentions
            







            // Start The window manager
            App.Start(this);
        }

        public void UpdateHandLocationOnScreen(Vector2 lefthand, Vector2 righthand)
        {
            UpdateLefthandPosition(righthand);
            UpdateRighthandPostion(lefthand);
        }

        public void UpdateRighthandPostion(Vector2 Point)
        {
            this.Dispatcher.Invoke(new Action(() => { Canvas.SetTop(this.righttmouse, Point.X); }));
        }

        public void UpdateLefthandPosition(Vector2 Point)
        {
            this.Dispatcher.Invoke(new Action(() => { Canvas.SetTop(this.leftmouse, Point.Y); }));
        }

        internal void AddWindowToStack(NuiWindow nui)
        {
            NuiWindowStack.Push(nui);

        }

        internal void ShowWindow(NuiWindow nui)
        {
            AddWindowToStack(nui);
            App.ActiveControl=(NuiWindow)NuiWindowStack.Peek();
            App.ActiveControl.Show();
        }

        internal void HideWindow(NuiWindow nui)
        {
            RemoveWindowFromStack();
            App.ActiveControl = (NuiWindow)NuiWindowStack.Peek();
            App.ActiveControl.Show();
        }

        private void RemoveWindowFromStack()
        {
            App.ActiveControl.Hide();
            NuiWindowStack.Pop();
        }

        internal void ShowDialogBox()
        {
            App.ActiveControl = (NuiWindow)NuiWindowStack.Peek();
            App.ActiveControl.ShowDialogBox();
        }

        internal NuiWindow GetActiveWindow()
        {
            return App.ActiveControl;
        }
        // Forows the Message to the Active Window
        internal void InterpreteMessage(NuiEvent nevent)
        {
            switch (nevent.MessageCode)
            {
                case (KinectWindows.Messages.WindowMessages.NUI_MSG_RIGHT_HAND_MOVED):
                    UpdateRighthandPostion((Vector2)nevent.Args[0]);
                    break;
                case (KinectWindows.Messages.WindowMessages.NUI_MSG_LEFT_HAND_MOVED):
                    UpdateLefthandPosition((Vector2)nevent.Args[0]);
                    break;


                default:
                    App.ActiveControl.PostMessage(nevent);
                    break;
            }

           
        }
    }
    
}
